package com.yvan.game.merge.support.mvc;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class JztdMvcAutoConfigration extends WebMvcConfigurerAdapter {

    /**
     * 默认输出 json
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

    /**
     * http 转换请求
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 拦截 http请求，转换为想要的数据
        // org.springframework.web.method.support.InvocableHandlerMethod:156 行
        argumentResolvers.add(new QueryParamResolver());
        argumentResolvers.add(new JsonBodyResolver());
        argumentResolvers.add(new PageDbResolver());
        argumentResolvers.add(new JsonWapperResolver());
        argumentResolvers.add(new ParameterParserResolver());
    }

    /**
     * 跨域支持
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*");
    }
}
